﻿using System;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;

namespace SimpleFileExplorer
{
    public partial class MainForm : Form
    {
        private MenuStrip menuStrip1;
        private ToolStrip toolStrip1;
        private SplitContainer splitContainer;
        private TreeView treeView1;
        private ListView listView1;
        private ToolStripButton toolStripButtonOpenFolder;

        public MainForm()
        {
            InitializeComponent();
            PopulateTreeView();
        }

        private void InitializeComponent()
        {
            this.menuStrip1 = new MenuStrip();
            this.toolStrip1 = new ToolStrip();
            this.splitContainer = new SplitContainer();
            this.treeView1 = new TreeView();
            this.listView1 = new ListView();
            this.toolStripButtonOpenFolder = new ToolStripButton();

            // Initialize menuStrip1
            var fileMenuItem = new ToolStripMenuItem("&File");
            this.menuStrip1.Items.Add(fileMenuItem);

            // Initialize toolStrip1
            this.toolStripButtonOpenFolder.Text = "Open Folder";
            this.toolStripButtonOpenFolder.Click += ToolStripButtonOpenFolder_Click;
            this.toolStrip1.Items.Add(this.toolStripButtonOpenFolder);

            // SplitContainer
            this.splitContainer.Dock = DockStyle.Fill;
            this.splitContainer.Orientation = Orientation.Vertical;
            this.splitContainer.Panel1.Controls.Add(this.treeView1);
            this.splitContainer.Panel2.Controls.Add(this.listView1);

            // TreeView
            this.treeView1.Dock = DockStyle.Fill;
            this.treeView1.AfterSelect += new TreeViewEventHandler(this.TreeView1_AfterSelect);

            // ListView
            this.listView1.Dock = DockStyle.Fill;
            this.listView1.View = View.Details;
            this.listView1.Columns.Add("Name", 300, HorizontalAlignment.Left);
            this.listView1.Columns.Add("Type", 150, HorizontalAlignment.Left);
            this.listView1.FullRowSelect = true;
            this.listView1.DoubleClick += new EventHandler(this.ListView1_DoubleClick);

            // MainForm
            this.MainMenuStrip = this.menuStrip1;
            this.Controls.Add(this.splitContainer);
            this.Controls.Add(this.toolStrip1);
            this.Controls.Add(this.menuStrip1);
            this.Text = "Simple File Explorer";
            this.Load += new EventHandler(this.MainForm_Load);

            this.StartPosition = FormStartPosition.CenterScreen;
        }

        private void ToolStripButtonOpenFolder_Click(object sender, EventArgs e)
        {
            var fbd = new FolderBrowserDialog();
            if (fbd.ShowDialog() == DialogResult.OK)
            {
                TreeNode rootNode = new TreeNode(fbd.SelectedPath) { Tag = new DirectoryInfo(fbd.SelectedPath) };
                treeView1.Nodes.Add(rootNode);
                PopulateTreeView();
            }
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            PopulateTreeView();
        }

        private void PopulateTreeView()
        {
            treeView1.Nodes.Clear();
            DirectoryInfo info = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile));
            if (info.Exists)
            {
                TreeNode rootNode = new TreeNode(info.Name) { Tag = info };
                treeView1.Nodes.Add(rootNode);
                GetDirectories(info.GetDirectories(), rootNode);
                rootNode.Expand();
            }
        }

        private void GetDirectories(DirectoryInfo[] subDirs, TreeNode nodeToAddTo)
        {
            TreeNode aNode;
            foreach (DirectoryInfo subDir in subDirs)
            {
                if ((subDir.Attributes & FileAttributes.ReparsePoint) != FileAttributes.ReparsePoint)
                {
                    aNode = new TreeNode(subDir.Name) { Tag = subDir };
                    nodeToAddTo.Nodes.Add(aNode);
                    try
                    {
                        DirectoryInfo[] subSubDirs = subDir.GetDirectories();
                        GetDirectories(subSubDirs, aNode);
                    }
                    catch (UnauthorizedAccessException)
                    {
                        aNode.Nodes.Add("Access Denied");
                    }
                }
            }
        }

        private void TreeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            listView1.Items.Clear();
            DirectoryInfo nodeDirInfo = (DirectoryInfo)e.Node.Tag;
            try
            {
                foreach (DirectoryInfo dir in nodeDirInfo.GetDirectories())
                {
                    listView1.Items.Add(new ListViewItem(new[] { dir.Name, "Directory" }));
                }

                foreach (FileInfo file in nodeDirInfo.GetFiles())
                {
                    listView1.Items.Add(new ListViewItem(new[] { file.Name, "File" }));
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"An error occurred: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void ListView1_DoubleClick(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)
            {
                ListViewItem item = listView1.SelectedItems[0];
                string fullPath = Path.Combine(((DirectoryInfo)treeView1.SelectedNode.Tag).FullName, item.Text);
                if (File.Exists(fullPath) && fullPath.EndsWith(".txt"))
                {
                    Process.Start("notepad.exe", fullPath);
                }
            }
        }
    }
}

